package model

import (
	"database/sql"

	"github.com/go-gorp/gorp"
	_ "github.com/go-sql-driver/mysql"
)

var (
	Db    *sql.DB
	Dbmap *gorp.DbMap
)

func InitDb() error {
	var err error
	Db, err = sql.Open("mysql", "root:wenzhenlin@tcp(localhost:3306)/goblog?charset=utf8")
	if err != nil {
		goto end
	}
	Db.SetMaxOpenConns(20)
	Db.SetMaxIdleConns(10)

	Dbmap = &gorp.DbMap{Db: Db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}

	Dbmap.AddTableWithName(Post{}, "post").SetKeys(true, "Id")
	Dbmap.AddTableWithName(Category{}, "category").SetKeys(true, "Id")
	Dbmap.AddTableWithName(Tags{}, "tags").SetKeys(true, "Id")

	err = Dbmap.CreateTablesIfNotExists()

	if err != nil {
		goto end
	}

end:
	if err != nil {
		if Db != nil {
			Db.Close()
		}
	}
	return err
}
